﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.Web.Components;
using System.Data;


namespace CardLogistics.Web.MainPage.CardArrival.OrderArrivalBook
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.CardArrival.OrderArrivalBookData DAL = new CardLogistics.DAL.CardArrival.OrderArrivalBookData();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                InitControl();

            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void InitControl()
        {
            if (IsEdit)
            {
                string _key = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(_key))
                {
                    CardLogistics.Model.CardArrival.OrderArrivalBook sData = DAL.GetModel(_key);
                    SetWebControls(this.Page, sData);
                    hf_OrderId.Value = sData.ORDERID;//合同编号
                    hf_CheckId.Value = sData.CHECKID; //入库ID
                }
            }
        }
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = "";
                if (IsEdit)
                {
                    result = DAL.Update(EncapsulationModel());
                }
                else
                    result = DAL.Add(EncapsulationModel());

                if (result.Split(',')[0] == "ok")
                {
                    ReturnListInfo();
                }
                else
                {
                    ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        /// <summary>
        /// 订到货管理台帐单实体
        /// </summary>
        /// <returns></returns>
        private CardLogistics.Model.CardArrival.OrderArrivalBook EncapsulationModel()
        {
            CardLogistics.Model.CardArrival.OrderArrivalBook model = new Model.CardArrival.OrderArrivalBook();
            model = GetWebControls<CardLogistics.Model.CardArrival.OrderArrivalBook>(this.Page);
            model.LASTUPDATEUSER = LoggedUser.ID;//最后修改人
            model.LASTUPDATEDATE = DateTime.Now; //最后修改时间
            model.ORDERID = hf_OrderId.Value;//订单ID
            model.CHECKID = hf_CheckId.Value; //入库ID

            if (IsEdit)
                model.ID = Request.QueryString["id"];
            return model;
        }
    }
}